// 此处定义全局函数
import moment from 'moment';

export default {
  formatPrice(value) {
    let val = (value).toFixed(2).replace(',', '.');
    return val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  },
  formatDate(dateStr) {
    if(!dateStr || dateStr.indexOf('1900-01-01')>=0) return '';
    return moment(dateStr).format('YYYY/MM/DD');
  },
  formatDate2(dateStr) {
    if(!dateStr || dateStr.indexOf('1900-01-01')>=0) return '';
    return moment(dateStr).format('YYYY-MM-DD');
  },
  formatMMDD(dateStr) {
    if(!dateStr || dateStr.indexOf('1900-01-01')>=0) return '';
    return moment(dateStr).format('MM月DD日');
  },
  formatMMDDHHmm(dateStr) {
    if(!dateStr || dateStr.indexOf('1900-01-01')>=0) return '';
    return moment(dateStr).format('MM-DD HH:mm');
  },
  formatHHmm(dateStr) {
    if(!dateStr || dateStr.indexOf('1900-01-01')>=0) return '';
    return moment(dateStr).format('HH:mm');
  },
  getChatTime(dateStr){
    if(dateStr.indexOf('1900-01-01')>=0){
      return ' ';
    }
    var days = moment().diff(moment(dateStr), 'days');
    if(days == 0){
      return moment(dateStr).format('HH:mm');
    } else if (days == 1){
      return '昨天';
    } else {
      return moment(dateStr).format('MM/DD');
    }
  },
  formatDateTime(dateStr) {
    if(!dateStr || dateStr.indexOf('1900-01-01')>=0) return '';
    return moment(dateStr).format('YYYY/MM/DD HH:mm:ss');
  },
  formatDateTime2(dateStr) {
    if(!dateStr || dateStr.indexOf('1900-01-01')>=0) return '';
    return moment(dateStr).format('YYYY-MM-DD HH:mm:ss');
  },
  formatDateTime3(dateStr) {
    if(!dateStr || dateStr.indexOf('1900-01-01')>=0) return '';
    return moment(dateStr).format('YYYYMMDDHHmmss');
  },
  formatYMDHM(dateStr) {
    if(!dateStr || dateStr.indexOf('1900-01-01')>=0) return '';
    return moment(dateStr).format('YYYY-MM-DD HH:mm');
  },
  now(){
    return moment().format();
  },
  lastMonthStart() {
    const startDate = moment().month(moment().month() - 1).startOf('month');
    return startDate.format('YYYY-MM-DD');
  },
  lastMonthEnd() {
    const endDate = moment().month(moment().month() - 1).endOf('month');
    return endDate.format('YYYY-MM-DD');
  },
  calcAge(birthday) {
    var birth = new Date(birthday);
    var ageDifMs = Date.now() - birth.getTime();
    var ageDate = new Date(ageDifMs);
    return Math.abs(ageDate.getUTCFullYear() - 1970);
  },
  guid() {
    function s4() {
      return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
    }
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
  },
  replaceBreakLine(str){
    return str.replace(/(?:\r\n|\r|\n)/g, '<br>');
  },
  
  chatServer() {
    // eslint-disable-next-line no-undef
    return process.env.CHAT_SERVER;
  },
  buildImageContent(imgUrl){
    var imgContent = '<img class="chatimage" src="' + imgUrl + '"></img>';
    return imgContent;
  },
  isImage(imgUrl){
    var reg = /^(http:|https:)(\w|:|\/|\.|:)+(\.jpg|\.jpeg|\.png)$/;
    return reg.test(imgUrl);
  },
  fuzzyMobile(mobile){
    if(!mobile){
      return '';
    }
    let re = /(\d{3})\d{4}(\d{4})/;
    let str = mobile.replace(re,'$1****$2');
    return str;
  }
};
